#coding=utf-8
import datetime
def now():
  return datetime.datetime.now()

def print_red(s):
  print '\033[0;31;48m%s\033[0m'%s  
  
import os, sys, subprocess
SCRIPT_ROOT = os.path.realpath(os.path.dirname(__file__))
PACKAGE_PATH = os.path.normpath(os.path.join(SCRIPT_ROOT, '../../'))
sys.path.append(PACKAGE_PATH)

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'emailserver_manager.settings'
from emailserver_manager.host.models import App, Account

PROJECT_DIR = os.path.expanduser('~/xiaojaylib/gae/mail_server2')

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-c', '--continue', action='store_true', dest='continue_',
                  default=False, help=u'中断后续传')

parser.add_option('-a', '--account', dest='account',
                  help=u'update 某个帐号下的Apps')
                  
parser.add_option('-r', '--cron', action='store_true', dest='cron',
                  default=False, help=u'仅 update cron')
                  
parser.add_option('-f', '--flag', action='store_true', dest='flag',
                  default=False, help=u'仅 update flag是True 的 app')
                  
(options, args) = parser.parse_args()
#print options.cron

def _update(apps, action):
  command = 'appcfg.py --passin --no_cookies --application=%s --email=%s %s %s'

  for app in apps:
    a = app.account
    complete_command = command%(app.name, a.email, action, PROJECT_DIR)
    
    print '\n'
    print_red(app.name)
    print complete_command, '\n'
    
    app.last_update_started = True
    try:
      p = subprocess.Popen(complete_command, shell=True, stdin=subprocess.PIPE)
      p.communicate(a.password)
    
      app.last_update_ended = True
      app.last_update_succeed_at = now()
    finally:
      app.save()

def update():
  apps = App.objects.order_by('created_at').all()
  for a in apps:
    a.last_update_started = False
    a.last_update_ended = False
    a.save()
    
  _update(apps, 'update')
     
def update_c():
  app = App.objects.order_by('created_at').filter(last_update_started=True).filter(last_update_ended=False)[0]
  a = app.account
  
  command = 'appcfg.py --passin --no_cookies --application=%s --email=%s rollback %s'
  complete_command = command%(app.name, a.email, PROJECT_DIR)
  p = subprocess.Popen(complete_command, shell=True, stdin=subprocess.PIPE)
  p.communicate(a.password)
  
  apps = App.objects.order_by('created_at').filter(last_update_ended=False).all()
  _update(apps, 'update')

def update_account(account):
  a = Account.objects.get(email=account)
  apps = a.app_set.all()
  _update(apps, 'update')

def update_cron():
  apps = App.objects.order_by('created_at').all()
  _update(apps, 'update_cron')

def update_f():
  apps = App.objects.filter(flag=True).order_by('created_at').all()
  _update(apps, 'update')
  
if options.cron:
  update_cron()
  sys.exit(1)

if options.account:
  update_account(options.account)
  sys.exit(1)
  
if options.continue_:
  update_c()
  sys.exit(1)

if options.flag:
  update_f()
  sys.exit(1)
update()
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
